package jee.boot.common.basic;

import jee.boot.common.utils.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目信息打印
 */
public class ProjectBasicInfo {
    private static final  Logger logger= LoggerFactory.getLogger(ProjectBasicInfo.class);
    public static void printProjectBasicInfo(){
        List<Attr> projectBasicInfo = getProjectBasicInfo();
        StringBuilder sb=new StringBuilder();
        sb.append("\r\n").append("----------------------项目启动成功----------------------").append("\r\n");
        for(Attr attr:projectBasicInfo){
            if(attr.value!=null){
                sb.append(getFormatString(30,attr.getName()));
                sb.append(getFormatString(50,attr.getKey()));
                sb.append(attr.getValue()).append("\r\n");
            }
        }
        sb.append("--------------------------------------------------------").append("\r\n");
        logger.info(sb.toString());
    }
    public static List<Attr> getProjectBasicInfo(){
        List<Attr> list=new ArrayList<>();
        list.add(new Attr("项目名称","project.name" ));
        list.add(new Attr("项目初始化","project.initData"));
        list.add(new Attr("项目多租户","project.enableTenant"));
        list.add(new Attr("项目版本","project.version"));
        list.add(new Attr("服务端口","server.port"));
        list.add(new Attr("开启自动刷新token","oauth.enableAutoRefreshToken"));
        list.add(new Attr("注册中心","eureka.client.serviceUrl.defaultZone"));
        list.add(new Attr("配置中心","spring.cloud.config.uri"));
        list.add(new Attr("日志级别","logging.level.jee.boot"));
        list.add(new Attr("数据库Schema","spring.schema"));
        list.add(new Attr("数据库连接url","spring.datasource.url"));
        list.add(new Attr("数据库连接user","spring.datasource.username"));
        list.add(new Attr("数据库连接池","spring.datasource.type"));
        list.add(new Attr("Redis集群","spring.redis.cluster.nodes"));
        if(SpringContextHolder.getBean(Environment.class).getProperty("spring.redis.cluster.nodes")==null){
            list.add(new Attr("Redis-Server","spring.redis.host"));
            list.add(new Attr("Redis-Port","spring.redis.port"));
        }
        list.add(new Attr("邮件Host","spring.mail.host"));
        list.add(new Attr("邮件发件人","spring.mail.username"));
        list.add(new Attr("消息队列","spring.activemq.broker-url"));
        list.add(new Attr("定时任务存储方式","spring.quartz.job-store-type"));
        list.add(new Attr("搜索引擎","spring.elasticsearch.server"));
        list.add(new Attr("后台任务服务","spring.thread-pool-config.enable"));
        return list;
    }
    private static String getFormatString(int length,String s1){
        int szCount = 0;//字符串中包含的数子,字母,符号的长度
        for (int i = 0; i < s1.length(); i++) {
            char c = s1.charAt(i);
            if (c >= 32 && c <= 133)
                szCount++;
           }
        int format = length-s1.length()+szCount;
        String a = "%-"+format +"s";
       return  String.format(a,s1);
    }


   static class Attr{
        private String name;
        private String key;
        private String value;

        public Attr(String name, String key) {
            this.name = name;
            this.key = key;
            this.value = getValue(key);
        }
        public Attr(String name, String key,String value) {
            this.name = name;
            this.key = key;
            this.value = value;
        }
       /**
        * 获取系统配置信息
        * @param key
        * @return
        */
       private static String getValue(String key){
           return SpringContextHolder.getBean(Environment.class).getProperty(key);
       }
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    public static void main(String[] args) {
            printProjectBasicInfo();
    }
}
